<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\BatchRestore;
use App\Admin\Actions\Grid\Restore;
use App\Admin\Repositories\Link;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LinkController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Link(), function (Grid $grid) {

            $grid->showColumnSelector();
            $grid->enableDialogCreate();
            $grid->setDialogFormDimensions('800px', '300px');
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableActions();


            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->text('title');
                $create->text('link');
            });

            $grid->column('id')->sortable()->hide();
            $grid->column('title')->editable();
            $grid->column('link')->editable();
            $grid->column('created_at')->sortable()->hide();
            $grid->column('updated_at')->sortable()->hide();
            $grid->column('skip', '打开')->display(function () {
                return '<a href="' . $this->link . '" target="_blank">打开</a>';
            });


            $grid->quickSearch('title');


            // 回收站
            $grid->filter(function (Grid\Filter $filter) {

                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();

                $filter->like('title');
                $filter->like('link');

            });
            // 行恢复
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Link::class));
                }
            });
            // 批量恢复
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Link::class));
                }
            });

        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Link(), function (Form $form) {

            $form->hidden('id');

            $form->text('title')->required()->rules('required');
            $form->url('link')->required()->rules('required|url|unique:links,link,' . request('id'));

        });
    }
}
